# 高斯分布的主成份分析
import argparse
from typing import Dict
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

class Chp01Sec05S2(object):
    def __init__(self):
        self.name = ''

    @staticmethod
    def startup(params:Dict = {}) -> None:
        print(f'高斯分布主成份分析 v0.0.1')
        plt.rcParams['figure.figsize'] = [16, 8]
        plt.rcParams.update({'font.size': 18})
        obs = np.loadtxt('study/ddse/supports/DATA_PYTHON/DATA/ovariancancer_obs.csv',delimiter=',')
        f = open('study/ddse/supports/DATA_PYTHON/DATA/ovariancancer_grp.csv', "r")
        grp = f.read().split("\n")
        print(f'grp: {type(grp)}; {len(grp)}; \n{grp};????????')
        U, S, VT = np.linalg.svd(obs,full_matrices=0)
        print(f'obs: {obs.shape}; U: {U.shape}; S: {S.shape}; VT: {VT.shape};')
        # obs: (216, 4000); U: (216, 216); S: (216,); VT: (216, 4000); 216个患者，4000个属性
        fig1 = plt.figure()
        ax1 = fig1.add_subplot(121)
        ax1.semilogy(S,'-o',color='k')
        ax2 = fig1.add_subplot(122)
        ax2.plot(np.cumsum(S)/np.sum(S),'-o',color='k')
        plt.show()
        fig2 = plt.figure()
        ax = fig2.add_subplot(111, projection='3d')
        ######################################################################################
        VT1 = VT[:3, :]
        X0 = (VT1 @ obs.T).T
        print(f'VT1: {VT1.shape}; obs.T: {obs.T.shape}; X0: {X0.shape};')
        plt.title('Get the PCA first')
        for j in range(obs.shape[0]):
            if grp[j] == 'Cancer':
                ax.scatter(X0[j][0], X0[j][1], X0[j][2],marker='x',color='r',s=50)
            else:
                ax.scatter(X0[j][0],X0[j][1],X0[j][2],marker='o',color='b',s=50)
        ax.view_init(25,20)
        plt.show()


def main(params:Dict = {}) -> None:
    Chp01Sec05S2.startup(params=params)

def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--run_mode', action='store',
        type=int, default=1, dest='run_mode',
        help='run mode'
    )
    return parser.parse_args()

if '__main__' == __name__:
    args = parse_args()
    params = vars(args)
    main(params=params)